using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using CsvCatcher.DataSchema;

namespace CsvCatcher
{
    partial class CsvCatcherImpl
    {
        private void PerformOperations(FileInfo fi, Rule rule)
        {
            foreach (OperationType op in rule.Operations)
            {
                DirectoryInfo fid = new DirectoryInfo(rule.Path);

                if (fid.Exists)
                {
                    string destpath = fid.FullName + fi.Name;

                    switch (op)
                    {
                        case OperationType.Copy:
                            fi.CopyTo(destpath);
                            _progressHandler.WriteToOutput("CsvCatcher. File " + fi.FullName + " copied to " + rule.Path, HunterPluginSDK.LogPriority.INFO);
                            break;
                        case OperationType.Move:
                            fi.MoveTo(destpath);
                            _progressHandler.WriteToOutput("CsvCatcher. File " + fi.FullName + " moved to " + rule.Path, HunterPluginSDK.LogPriority.INFO);
                            break;
                        case OperationType.Unpack:
                            FileInfo dest = new FileInfo(destpath);
                            if (dest.Exists)
                            {
                                string InputPathOfZipFile = dest.FullName;
                                string baseDirectory = Path.GetDirectoryName(dest.DirectoryName);

                                using (ZipInputStream ZipStream = new ZipInputStream(File.OpenRead(InputPathOfZipFile)))
                                {
                                    ZipEntry theEntry;
                                    while ((theEntry = ZipStream.GetNextEntry()) != null)
                                    {
                                        if (theEntry.IsFile)
                                        {
                                            if (theEntry.Name != "")
                                            {
                                                string strNewFile = @"" + baseDirectory + @"\" +  theEntry.Name;
                                                if (File.Exists(strNewFile))
                                                {
                                                    continue;
                                                }

                                                using (FileStream streamWriter = File.Create(strNewFile))
                                                {
                                                    int size = 2048;
                                                    byte[] data = new byte[2048];
                                                    while (true)
                                                    {
                                                        size = ZipStream.Read(data, 0, data.Length);
                                                        if (size > 0)
                                                            streamWriter.Write(data, 0, size);
                                                        else
                                                            break;
                                                    }
                                                    streamWriter.Close();
                                                }
                                            }
                                        }
                                        else if (theEntry.IsDirectory)
                                        {
                                            string strNewDirectory = @"" + baseDirectory + @"\" + theEntry.Name;
                                            if (!Directory.Exists(strNewDirectory))
                                            {
                                                Directory.CreateDirectory(strNewDirectory);
                                            }
                                        }
                                    }
                                    ZipStream.Close();
                                }
                            }  

                            break;
                    }

                    

                }
                else
                    _progressHandler.WriteToOutput("CsvCatcher. Can't copy/move to dir " + fid.FullName, HunterPluginSDK.LogPriority.ERROR);

            }
        }
    }
}
